import {
    validate,
    Length
} from 'class-validator'

import BigNumber from 'bignumber.js'

import * as utils from '@app/methods/utils'
import { BalanceManager } from '@app/balances'

class Request {
    @Length(3, 8)
    symbol: string = 'TRX'
}

/**
 * 获取所有钱包地址的余额
 */
export async function getWalletBalance(balances: BalanceManager, params: any, callback: any) {
    const model = utils.makeModel(new Request, params)
    const errors = await validate(model)
    if (errors.length > 0) {
        callback(utils.BadRequest2(errors), undefined)
        return
    }

    const balanceSheet = balances.getBalanceSheet(model.symbol)
    if (!balanceSheet) {
        callback(utils.InvalidToken, undefined)
        return
    }

    let totalBalance = new BigNumber(0)
    await balanceSheet.forEach(async (address: string, balance: BigNumber) => {
        totalBalance = totalBalance.plus(balance)
        return true
    })
    callback(undefined, totalBalance.toString())
}
